﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.Resources;
using KZ.Lottery.BLL;

namespace KZ.Lottery
{
    public partial class FrmGroupGroupInsert : Form
    {
        #region Private Methods

        private void FrmGroupGroupInsert_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGroupGroup();
                LoadNotGroup();
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
        }

        private void LoadGroupGroup()
        {
            try
            {
                bdsGroupGroup.DataSource = GroupGroupBLL.GetGroupGroupByCode(GroupCode);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadGroupGroup", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void LoadNotGroup()
        {
            try
            {
                string groupID = Global.GetDtgTotalValue(dtgGroupGroup, colGroupID.Index);
                if (String.IsNullOrEmpty(groupID)) groupID = "'-1'";
                bdsNotGroup.DataSource = GroupBLL.GetGroupNotGroupList(groupID);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadNotGroup", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void dtgNotGroup_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataTable dtNotGroup = bdsNotGroup.DataSource as DataTable;
            DataTable dtGroupGroup = bdsGroupGroup.DataSource as DataTable;

            DataRow row = dtGroupGroup.NewRow();
            row["GroupID"] = dtNotGroup.Rows[e.RowIndex]["GroupID"];
            row["GroupCode"] = GroupCode;

            dtGroupGroup.Rows.Add(row);
            LoadNotGroup();
        }

        private void dtgGroupGroup_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataTable dtGroupGroup = bdsGroupGroup.DataSource as DataTable;
            
            dtgGroupGroup.Rows.RemoveAt(e.RowIndex);
            LoadNotGroup();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                GroupGroupBLL ggBLL = new GroupGroupBLL();
                DataTable dt = bdsGroupGroup.DataSource as DataTable;
                if (Action == KZ_Resource.Action_Insert)
                {
                    if (ggBLL.InsertGroupGroup(dt))
                    {
                        LoadGroupGroup();
                        LoadNotGroup();
                    }
                }
                else
                {
                    //if (resultBLL.UpdateResult(Result))
                    //{
                    //    Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RULE_UPDATE, true);
                    //    cmbChannelID.Focus();
                    //    _IsSaved = true;
                    //    dtpResultDate_ValueChanged(dtpResultDate, null);
                    //}
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnSave_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        #endregion

        #region Constructors
        public FrmGroupGroupInsert()
        {
            InitializeComponent();
        } 
        #endregion

        #region Public Properties

        public string Action { get; set; }

        public string GroupCode { get; set; }

        #endregion
    }
}
